package com.CNMedical.system.service.impl;

import java.util.List;

import com.CNMedical.common.core.domain.R;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diboot.core.binding.Binder;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.system.domain.vo.AppletKnowledgeCollectVO;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletKnowledgeCollectMapper;
import com.CNMedical.system.domain.entity.AppletKnowledgeCollect;
import com.CNMedical.system.service.IAppletKnowledgeCollectService;

import javax.annotation.Resource;

/**
 * 知识收藏Service业务层处理
 * 
 * @author tang
 * @date 2023-03-28
 */
@Service
public class AppletKnowledgeCollectServiceImpl extends BaseServiceImpl<AppletKnowledgeCollectMapper, AppletKnowledgeCollect> implements IAppletKnowledgeCollectService
{
    @Resource
    private AppletKnowledgeCollectMapper appletKnowledgeCollectMapper;

    @Override
    public boolean cancelKnowledgeCollect(String knowledgeId, String userId) {
        return lambdaUpdate().eq(AppletKnowledgeCollect::getKnowledgeId,knowledgeId)
                .eq(AppletKnowledgeCollect::getUserId,userId).remove();
    }

    @Override
    public R addKnowledgeCollect(AppletKnowledgeCollect appletKnowledgeCollect) {
        if (!lambdaQuery().eq(AppletKnowledgeCollect::getKnowledgeId,appletKnowledgeCollect.getKnowledgeId())
                .eq(AppletKnowledgeCollect::getUserId,appletKnowledgeCollect.getUserId()).exists()) {
            AppletKnowledgeCollect build = AppletKnowledgeCollect.builder().knowledgeId(appletKnowledgeCollect.getKnowledgeId())
                    .userId(appletKnowledgeCollect.getUserId()).build();
            return R.ok(this.save(build));
        }
        return R.fail();
    }

    @Override
    public Boolean isCollect(String knowledgeId, String userId) {
        return lambdaQuery().eq(AppletKnowledgeCollect::getKnowledgeId,knowledgeId)
                .eq(AppletKnowledgeCollect::getUserId,userId).exists();
    }

    @Override
    public List<AppletKnowledgeCollectVO> getKnowledgeCollectListByUserId(String userId) {
        List<AppletKnowledgeCollectVO> appletForumCollectVOList = getViewObjectList(Wrappers.lambdaQuery(AppletKnowledgeCollect.class)
                        .eq(AppletKnowledgeCollect::getUserId, userId),
                null,
                AppletKnowledgeCollectVO.class);
        return appletForumCollectVOList;
    }

    /**
     * 查询知识收藏
     * 
     * @param id 知识收藏主键
     * @return 知识收藏
     */
    @Override
    public AppletKnowledgeCollect selectAppletKnowledgeCollectById(Long id)
    {
        return appletKnowledgeCollectMapper.selectAppletKnowledgeCollectById(id);
    }

    /**
     * 查询知识收藏列表
     * 
     * @param appletKnowledgeCollect 知识收藏
     * @return 知识收藏
     */
    @Override
    public List<AppletKnowledgeCollectVO> selectAppletKnowledgeCollectList(AppletKnowledgeCollect appletKnowledgeCollect)
    {
        List<AppletKnowledgeCollect> appletKnowledgeCollects = appletKnowledgeCollectMapper.selectAppletKnowledgeCollectList(appletKnowledgeCollect);
        List<AppletKnowledgeCollectVO> appletKnowledgeCollectVOS = Binder.convertAndBindRelations(appletKnowledgeCollects, AppletKnowledgeCollectVO.class);
        return appletKnowledgeCollectVOS;
    }

    /**
     * 新增知识收藏
     * 
     * @param appletKnowledgeCollect 知识收藏
     * @return 结果
     */
    @Override
    public int insertAppletKnowledgeCollect(AppletKnowledgeCollect appletKnowledgeCollect)
    {
        appletKnowledgeCollect.setCreateTime(DateUtils.getNowDate());
        return appletKnowledgeCollectMapper.insertAppletKnowledgeCollect(appletKnowledgeCollect);
    }

    /**
     * 修改知识收藏
     * 
     * @param appletKnowledgeCollect 知识收藏
     * @return 结果
     */
    @Override
    public int updateAppletKnowledgeCollect(AppletKnowledgeCollect appletKnowledgeCollect)
    {
        appletKnowledgeCollect.setUpdateTime(DateUtils.getNowDate());
        return appletKnowledgeCollectMapper.updateAppletKnowledgeCollect(appletKnowledgeCollect);
    }

    /**
     * 批量删除知识收藏
     * 
     * @param ids 需要删除的知识收藏主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeCollectByIds(Long[] ids)
    {
        return appletKnowledgeCollectMapper.deleteAppletKnowledgeCollectByIds(ids);
    }

    /**
     * 删除知识收藏信息
     * 
     * @param id 知识收藏主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeCollectById(Long id)
    {
        return appletKnowledgeCollectMapper.deleteAppletKnowledgeCollectById(id);
    }
}
